/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hualu.zks.GUI;

import com.hualu.zks.Model.Node;
import com.hualu.zks.Dao.NodeDao;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;


/**
 * 
 * @author Administrator
 */
public class JFrame_Node extends javax.swing.JFrame {
   private static MainWindow mainJFrame=new MainWindow();
   public static ArrayList<Node> nodeList;//缓存在程序中
   public static NodeDao nodeDao;

	// NoteDirectory noteDirectory;
	// NoteDirectory noteDirectory = new NoteDirectory ();
	/**
	 * Creates new form NoteJFrame
	 */
	public JFrame_Node(MainWindow mainJFrame) {
                this.mainJFrame=mainJFrame;
                nodeDao =new NodeDao();
                nodeList = nodeDao.getAllNode();
		initComponents();
		Refresh();
	}

	public void Refresh() {
		// int rowCount = jTable1.getRowCount();
            nodeList=nodeDao.getAllNode();
		int rowCount = ((DefaultTableModel) jTable1.getModel()).getRowCount();
		for (int i = rowCount - 1; i >= 0; i--) {
			((DefaultTableModel) jTable1.getModel()).removeRow(i);
		}

		for (int i = 0; i < nodeList.size(); i++) {
			// Object [][]row = new Object[0][2];
			// row[0] = note.getName();
			// row[1] = note.getIPAdd();
			Vector vector = new Vector();
			vector.add(nodeList.get(i).getName());
			vector.add(nodeList.get(i).getIPAdd());
			((DefaultTableModel) jTable1.getModel()).addRow(vector);
		}
                if (jTable1.getSelectedRow()==-1&&jTable1.getRowCount()>=1) {
                    jTable1.setRowSelectionInterval(0,0);   
                }
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton_addnote = new javax.swing.JButton();
        jButton_change = new javax.swing.JButton();
        jButton_delete = new javax.swing.JButton();
        jButton_return = new javax.swing.JButton();
        jLabel_biaoti2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("节点设置");
        setIconImage(Toolkit.getDefaultToolkit().getImage("pic\\bfhl.jpg"));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jButton_addnote.setText("增加");
        jButton_addnote.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_addnoteMouseClicked(evt);
            }
        });
        jButton_addnote.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_addnoteActionPerformed(evt);
            }
        });

        jButton_change.setText("修改");
        jButton_change.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_changeMouseClicked(evt);
            }
        });
        jButton_change.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_changeActionPerformed(evt);
            }
        });

        jButton_delete.setText("删除");
        jButton_delete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_deleteMouseClicked(evt);
            }
        });
        jButton_delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_deleteActionPerformed(evt);
            }
        });

        jButton_return.setText("返回");
        jButton_return.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_returnMouseClicked(evt);
            }
        });
        jButton_return.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_returnActionPerformed(evt);
            }
        });

        jLabel_biaoti2.setFont(new java.awt.Font("华文行楷", 0, 24)); // NOI18N
        jLabel_biaoti2.setForeground(new java.awt.Color(240, 240, 240));
        jLabel_biaoti2.setText("节点管理");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "节点名称", "IP地址"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                jTable1AncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(400, 400, 400)
                        .addComponent(jLabel_biaoti2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton_addnote, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(32, 32, 32)
                                .addComponent(jButton_change, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addComponent(jButton_delete, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31)
                                .addComponent(jButton_return, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 457, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(39, 39, 39))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel_biaoti2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_return)
                    .addComponent(jButton_delete)
                    .addComponent(jButton_addnote)
                    .addComponent(jButton_change))
                .addGap(12, 12, 12))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_deleteActionPerformed
        // TODO add your handling code here:
        nodeDao.delNode(nodeList.get(jTable1.getSelectedRow()));
	Refresh();
    }//GEN-LAST:event_jButton_deleteActionPerformed

    private void jButton_deleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_deleteMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton_deleteMouseClicked

    private void jTable1AncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_jTable1AncestorAdded
        // TODO add your handling code here:
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				if (jTable1.getValueAt(jTable1.getSelectedRow(), 0) != null) {

				}
			}
		});

    }//GEN-LAST:event_jTable1AncestorAdded

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.dispose();
        mainJFrame.setVisible(true);
    }//GEN-LAST:event_formWindowClosing

	private void jButton_addnoteActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_addnoteActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton_addnoteActionPerformed

	private void jButton_addnoteMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton_addnoteMouseClicked
		// TODO add your handling code here:
		this.setVisible(false);
		new JFrame_NodeAdd(this).setVisible(true);
	}// GEN-LAST:event_jButton_addnoteMouseClicked

	private void jButton_changeMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton_changeMouseClicked
		// TODO add your handling code here:
		this.setVisible(false);
		JFrame_NodeEdit changeNode = new JFrame_NodeEdit(nodeList.get(jTable1.getSelectedRow()),this);
		changeNode.setVisible(true);
	}// GEN-LAST:event_jButton_changeMouseClicked
	
	

	private void jButton_returnMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton_returnMouseClicked
		// TODO add your handling code here:
		this.setVisible(false);
		mainJFrame.setVisible(true);
	}// GEN-LAST:event_jButton_returnMouseClicked

	private void jButton_returnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_returnActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton_returnActionPerformed

	private void jButton_changeActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_changeActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton_changeActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(JFrame_Node.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(JFrame_Node.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(JFrame_Node.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(JFrame_Node.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new JFrame_Node(mainJFrame).setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_addnote;
    private javax.swing.JButton jButton_change;
    private javax.swing.JButton jButton_delete;
    private javax.swing.JButton jButton_return;
    private javax.swing.JLabel jLabel_biaoti2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
